package es.udc.emarketapp.emarket.model.seller.dao;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.emarketapp.emarket.model.seller.vo.SellerVO;
import es.udc.emarketapp.emarket.model.user.vo.UserVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

public class MySQLSellerDAO extends AbstractSQLSellerDAO {

    public SellerVO create(Connection connection, SellerVO sellerVO)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;

        try {

            //Creamos el statement
            String query = "INSERT INTO usuario (id_usuario, pass, f_alta, "
                    + "ultima_act, aprobado, valido, nom_pila, ap1, ap2, nif_cif, dir, "
                    + "ciudad, cp, prov, pais, email, tlf1, tlf2, tipo_usuario) "
                    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            preparedStatement = connection.prepareStatement(query);

            //Ahora lo llenamos con los parametros que se quiere
            int i = 1;
            preparedStatement.setString(i++, sellerVO.getIdVendedor());
            preparedStatement.setString(i++, sellerVO.getPass());
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setTimestamp(i++,
                    new Timestamp(Calendar.getInstance().getTime().getTime()));
            preparedStatement.setBoolean(i++, sellerVO.isAprobado());
            preparedStatement.setBoolean(i++, sellerVO.isValido());
            preparedStatement.setString(i++, sellerVO.getNomPila());
            preparedStatement.setString(i++, sellerVO.getAp1());
            preparedStatement.setString(i++, sellerVO.getAp2());
            preparedStatement.setString(i++, sellerVO.getNifCif());
            preparedStatement.setString(i++, sellerVO.getDir());
            preparedStatement.setString(i++, sellerVO.getCiudad());
            preparedStatement.setString(i++, sellerVO.getCp());
            preparedStatement.setString(i++, sellerVO.getProv());
            preparedStatement.setString(i++, sellerVO.getPais());
            preparedStatement.setString(i++, sellerVO.getEmail());
            preparedStatement.setString(i++, sellerVO.getTlf1());
            preparedStatement.setString(i++, sellerVO.getTlf2());
            preparedStatement.setString(i++, sellerVO.getTipoUsuario());


            // Execute query.
            int insertedRows = preparedStatement.executeUpdate();

            if (insertedRows == 0) {
                throw new SQLException("No se pudo insertar la fila a la tabla"
                        + " 'Usuario'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Fila duplicada en la tabla 'Usuario'");
            }


            //*************************

            //Creamos el statement
            query = "INSERT INTO vendedor (id_vendedor, nom_emp, "
                    + "n_reg_merc, fax, iban, swift) "
                    + "VALUES (?, ?, ?, ?, ?, ?)";

            preparedStatement = connection.prepareStatement(query);

            //Ahora lo llenamos con los parametros que se quiere
            i = 1;
            preparedStatement.setString(i++, sellerVO.getIdVendedor());
            preparedStatement.setString(i++, sellerVO.getNomEmp());
            preparedStatement.setString(i++, sellerVO.getnRegMerc());
            preparedStatement.setString(i++, sellerVO.getFax());
            preparedStatement.setString(i++, sellerVO.getIban());
            preparedStatement.setString(i++, sellerVO.getSwift());


            // Execute query.
            insertedRows = preparedStatement.executeUpdate();

            if (insertedRows == 0) {
                throw new SQLException("No se pudo insertar la fila a la tabla"
                        + " 'Vendedor'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Fila duplicada en la tabla 'Vendedor'");
            }


            return sellerVO;

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }

    }


}
